package com.superm.controller;

import com.superm.entity.*;
import com.superm.service.impl.MemberLeveServiceImpl;
import com.superm.service.impl.MemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberServiceImpl memberService;

    /**
     * 会员等级实现类
     */
    @Autowired
    private MemberLeveServiceImpl memberLeveService;


    @RequestMapping("/members")
    public ModelAndView members(){
        ModelAndView mv = new ModelAndView();
        //查询所有卡等级信息
        List<MemberLeve> memberLeveList =   memberLeveService.selectMemberLeveList();
        mv.addObject("memberLeveList",memberLeveList);
        mv.setViewName("members/MemberList");
        return mv;
    }

    /**
     * 模糊查询会员信息
     * @return
     */
    @RequestMapping("/selectMember")
    @ResponseBody  //返回真实数据
    public Map<String,Object> selectMember(Member member, @RequestParam("page") Integer pageNo, @RequestParam("limit")Integer pageSize){
        System.out.println(member);
        Map<String,Object> map = new HashMap<String,Object>();
        List<Member> membersList =  memberService.selectLikeMemberList(member,pageNo,pageSize);
        //获取总条数
        Integer count = memberService.selectMemberCount(member);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",membersList);
        return map;
    }

    /**
     * 跳转到会员添加页面
     * @return
     */
    @GetMapping ("/addMembers")
    public ModelAndView addMembers(){
        ModelAndView mv = new ModelAndView("members/addMember");
        //查询所有卡等级信息
        List<MemberLeve> memberLeveList =   memberLeveService.selectMemberLeveList();
        System.out.println(memberLeveList);
        mv.addObject("memberLeveList",memberLeveList);
        return mv;
    }

    /**
     * 会员信息添加
     * @return
     */
    @PostMapping("/addMembers")
    @ResponseBody
    public Map<String,Object> addMembers(Member member){
        Map<String,Object> map = new HashMap<String,Object>();
        if (memberService.addMember(member)>0){
            map.put("success",true);
            map.put("msg","添加成功");
        }else {
            map.put("success",false);
            map.put("msg","添加失败");
        }
        return map;
    }


    /**
     * 跳转到会员修改页面
     * @return
     */
    @GetMapping("/updateMembers")
    public ModelAndView updateMembers(Integer memberId){
        ModelAndView mv = new ModelAndView("members/updateMember");
        //查询所有卡等级信息
        List<MemberLeve> memberLeveList =   memberLeveService.selectMemberLeveList();
        //查询指定用户的信息
        Member member1 =   memberService.selectIdMember(memberId);
        mv.addObject("memberLeveList",memberLeveList);
        mv.addObject("member",member1);
        return mv;
    }

    /**
     * 会员信息修改
     * @return
     */
    @PostMapping("/updateMembers")
    @ResponseBody
    public Map<String,Object> updateMember(Member member){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (memberService.updateMember(member)>0){
            map.put("success",true);
            map.put("msg","修改成功");
        }else {
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }




    /**
     * 删除用户信息
     * @param memberId
     * @return
     */
    @RequestMapping("/deleteMember")
    @ResponseBody
    public Map<String,Object>deleteMember(@RequestParam("memberId")Integer memberId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (memberService.deleteMember(memberId)>0){
            map.put("success",true);
            map.put("msg","删除成功");
        }else{
            map.put("success",false);
            map.put("msg","删除失败");
        }
        return  map;
    }

    /**
     * 用户状态修改
     * @param memberId
     * @param memstate
     * @return
     */
    @RequestMapping("/updateOneMember")
    @ResponseBody
    public Map<String,Object> updateOneMember(@RequestParam("memberId")Integer memberId,@RequestParam("memstate")String memstate){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (memberService.updateOneMember(memberId,memstate)>0){
            //从认证主体获取用户Id,如果相同执行退出
            map.put("success",true);
            map.put("msg","修改成功");
        }else{
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }
    @RequestMapping("/selMember")
    @ResponseBody
    public  Map<String,Object> selMember(Member member){
        System.out.println("svvvvvvvvvvvvvvvvvvvvv");
        System.out.println(member);
        Map<String,Object> map = new HashMap<String,Object>();
            Member member1=  memberService.selMember(member);
            System.out.println(member1);
                if(member1!=null) {
                        map.put("success", true);
                        map.put("data",member1);

                }else{
                    map.put("success", false);
                    map.put("msg", "账号或密码错误！！！");
                }
        return map;
    }

}
